package com.guashu.jaywayjsonpath.renhang;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TableCreator {

    // 此方法接收字段信息的Map，并生成创建表的SQL语句
    public static List<String> generateCreateTableSQL(Map<String, List<FieldInfo>> tablesInfo) {
        List<String> createTableSQLList = new ArrayList<>();

        for (Map.Entry<String, List<FieldInfo>> tableEntry : tablesInfo.entrySet()) {
            String tableName = tableEntry.getKey();
            List<FieldInfo> fields = tableEntry.getValue();

            StringBuilder createTableSQL = new StringBuilder();
            createTableSQL.append("CREATE TABLE `").append(tableName).append("` (\n")
                    .append("  `id` BIGINT NOT NULL AUTO_INCREMENT,\n"); // 添加id字段，自动增长，作为主键

            // 遍历字段列表，为每个字段添加SQL语句
            for (FieldInfo field : fields) {
                createTableSQL.append("  `").append(field.getRuleKey()).append("` ")
                        .append(field.getDataType()).append(" COMMENT '")
                        .append(field.getDescription()).append("',\n");
            }

            // 添加create_time和update_time字段
            createTableSQL.append("  `pa01bi01` varchar(255) COMMENT '被查询者身份证号',\n")
                    .append("  `create_time` DATETIME  NULL DEFAULT CURRENT_TIMESTAMP,\n")
                    .append("  `update_time` DATETIME  NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n");

            // 设置id字段为主键
            createTableSQL.append("  PRIMARY KEY (`id`)\n");

            // 结束括号和分号来完成创建表的SQL语句
            createTableSQL.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n");

            // 将创建表的SQL语句添加到列表中
            createTableSQLList.add(createTableSQL.toString());
        }

        return createTableSQLList;
    }


    public static void main(String[] args) {
        // 连接到数据库
        String connectionUrl = "jdbc:mysql://localhost:3306/renhang?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
        try (Connection conn = DriverManager.getConnection(connectionUrl, "root", "root")) {
            DatabaseUtil dbUtil = new DatabaseUtil();
            Map<String, List<FieldInfo>> tableStructureInfo = dbUtil.queryTableStructure("business_parse_rule_renhang_v4");

            // 假设这个方法生成了创建表的SQL语句列表
            List<String> createTableSQLList = generateCreateTableSQL(tableStructureInfo);

            // 执行创建表的SQL语句
            for (String sql : createTableSQLList) {
                try (Statement stmt = conn.createStatement()) {
                    stmt.execute(sql);
                    System.out.println("Executed: " + sql);
                } catch (SQLException e) {
                    System.err.println("Failed to execute: " + sql);
                    e.printStackTrace();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
